package e2e

import (
	"fmt"
	"testing"

	"github.com/lab-online/pkg/auth"
	"github.com/lab-online/pkg/e2e"
	"github.com/lab-online/pkg/resp"
	"github.com/stretchr/testify/assert"
)

type searchUserReqQuery struct {
	Name     string `json:"name"`
	Username string `json:"username"`
	Role     string `json:"role"`
}

type searchUserRespDataItem struct {
	Name     string    `json:"name"`
	Username string    `json:"username"`
	Role     auth.Role `json:"role"`
}

type searchUserResp = resp.SuccessRespBody[[]searchUserRespDataItem]

func (c *context) defineSearchUserRequest(
	username string, role auth.Role,
	query *searchUserReqQuery,
) *e2e.Request {
	return &e2e.Request{
		Method: "GET",
		Path:   c.Prefix + "/v1/users",
		Query: map[string]string{
			"name":     query.Name,
			"username": query.Username,
			"role":     query.Role,
		},
		Header: map[string]string{
			"Authorization": c.GenBearerToken(username, role),
		},
	}
}

func (ctx *context) CaseSearchUser1() (string, func(*testing.T)) {
	ctx.JSONRequest(ctx.defineImportStudentRequest("teacher1", auth.RoleTeacher,
		[]importStudentReqDataItem{
			{"杏鸣for search", "00240304", "12345678"},
			{"杏鸣for search", "01240304", "12345678"},
		}))

	request := ctx.defineSearchUserRequest("admin1", auth.RoleAdmin, &searchUserReqQuery{
		Name: "杏鸣for search",
	})
	return "按姓名搜索", func(t *testing.T) {
		response := ctx.JSONRequest(request)

		assert.Equal(t, 200, response.Code)

		body := e2e.JSONBodyParser[searchUserResp](response)
		fmt.Println(body)

		assert.Equal(t, 20106, body.Code)
		assert.Equal(t, 2, len(body.Data))
		assert.ElementsMatch(t, []string{"00240304", "01240304"},
			[]string{body.Data[0].Username, body.Data[1].Username})
	}
}
